﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class LoginVIewModel : ViewModelBase
    {
        public AppData AppData { get; private set; } = AppData.Instance;

        private RelayCommand loginCommand;
        private RelayCommand cancelCommand;
        public RelayCommand LoginCommand
        {
            //get { return loginCommand; }
            get
            {
                return new RelayCommand(() =>
                {
                    MemberProvider memberProvider = new MemberProvider();
                    var list = memberProvider.Select(); // 获取所有用户数据
                    var user = list
                    .FirstOrDefault(item => item.Name == AppData.CurrentUser.Name
                    && item.Password == AppData.CurrentUser.Password);
                    if (user == null)
                    {
                        MessageBox.Show("用户名或密码错误，请重试！", "登录失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    else
                    {
                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                    }
                    // Here you would typically validate the login credentials
                    // For now, we just show a message box
                    //if (Member != null && !string.IsNullOrEmpty(Member.Name) && !string.IsNullOrEmpty(Member.Password))
                    //{
                    //    MessageBox.Show($"Login successful for user: {Member.Name}");
                    //}
                    //else
                    //{
                    //    MessageBox.Show("Please enter valid credentials.");
                    //}
                });
            }
        }//public RelayCommand LoginCommand

        public RelayCommand CancelCommand
        {
            //get { return cancelCommand; }
            get
            {
                return new RelayCommand(() =>
                {
                    // Here you would typically clear the login form or close the login window
                    //MessageBox.Show("Login cancelled.");
                    App.Current.Shutdown(); // Close the application
                });
            }
        }

        public LoginVIewModel()
        {
            AppData.CurrentUser.Name = "admin"; // 设置默认用户名
            AppData.CurrentUser.Password = "0"; // 设置默认密码
            //loginCommand = new RelayCommand(()=>
            //{
            //    MessageBox.Show("Login command executed.");
            //});
            //cancelCommand = new RelayCommand(()=>
            //{
            //    MessageBox.Show("cancel command executed.");
            //});
        }//public LoginVIewModel()

        public RelayCommand<Window> LoginCommand2
        {
            //get { return loginCommand; }
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    MemberProvider memberProvider = new MemberProvider();
                    var list = memberProvider.Select(); // 获取所有用户数据
                    var user = list
                    .FirstOrDefault(item => item.Name == AppData.CurrentUser.Name
                    && item.Password == AppData.CurrentUser.Password);
                    if (user == null)
                    {
                        MessageBox.Show("用户名或密码错误，请重试！", "登录失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    else
                    {
                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                        arg.Close(); // 关闭登录窗口
                    }
                });
            }
        }//public RelayCommand LoginCommand

    }
}
